<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入用户账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择用户性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="证件号" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入证件号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['customer:management:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['customer:management:remove']"
        >解绑
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerBindList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="真实姓名" align="center" prop="realName"/>
      <el-table-column label="关系" align="center" prop="relationsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.relations_type" :value="scope.row.relationsType"/>
        </template>
      </el-table-column>
      <el-table-column label="用户账号" align="center" prop="account"/>
      <el-table-column label="用户昵称" align="center" prop="nickName"/>
      <el-table-column label="用户邮箱" align="center" prop="email"/>
      <el-table-column label="手机号码" align="center" prop="phoneNumber"/>
      <el-table-column label="用户性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="头像url" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-avatar
            shape="square"
            size="small"
            :src="scope.row.avatar"
          />
        </template>
      </el-table-column>
<!--      <el-table-column label="帐号状态" align="center" prop="status">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
                </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      -->
      <el-table-column label="身份证类型" align="center" prop="idType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.id_type" :value="scope.row.idType"/>
        </template>
      </el-table-column>
      <el-table-column label="证件号" align="center" prop="idNumber"/>
      <el-table-column label="紧急联系人名字" align="center" prop="guardianName"/>
      <el-table-column label="紧急联系人手机" align="center" prop="guardianPhone"/>
      <el-table-column label="家庭住址" align="center" prop="address"/>
      <el-table-column label="真实姓名" align="center" prop="realName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['customer:management:remove']"
          >解绑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :visible.sync="popupVisible" title="客户列表" width="90%" append-to-body @close='closeBind'>
      <CustomerSelect :customer-id="form.customerId" :popupVisible="popupVisible"></CustomerSelect>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomerBind, getCustomerBind, delCustomerBind, addCustomerBind, updateCustomerBind } from "@/api/customer/customerBind";
import CustomerSelect  from '@/components/CustomerSelect'

export default {
  components:{
    CustomerSelect
  },
  name: "CustomerBind",
  dicts: ['sys_user_sex', 'sys_normal_disable', 'id_type','customer_bind_status', 'delete_flag','relations_type'],
  data() {
    return {
      popupVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家属绑定表格数据
      customerBindList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        account: null,
        nickName: null,
        phoneNumber: null,
        sex: null,
        status: null,
        idNumber: null,
        realName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: "绑定用户id不能为空", trigger: "blur" }
        ],
        bindCustomerId: [
          { required: true, message: "被绑定用户id不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "绑定状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    const customerId=this.$route.params && this.$route.params.customerId
    this.form.customerId=customerId
    this.queryParams.customerId=customerId
    this.getList();
  },
  methods: {
    closeBind(){
      this.popupVisible=false
      this.getList()
    },
    /** 查询家属绑定列表 */
    getList() {
      this.loading = true;
      listCustomerBind(this.queryParams).then(response => {
        this.customerBindList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        bindCustomerId: null,
        createTime: null,
        status: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.popupVisible = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomerBind(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改家属绑定";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除家属绑定编号为"' + ids + '"的数据项？').then(function() {
        return delCustomerBind(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('customer/customerBind/export', {
        ...this.queryParams
      }, `customerBind_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
